package com.luhaoran.hrm.handler;

/**
 * @author haoranlu
 * @project IntelliJ IDEA
 */

import com.luhaoran.hrm.exception.MyJsonValidationException;
import com.luhaoran.hrm.exception.TokenValidationException;
import com.luhaoran.hrm.utils.ViewUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常控制器
 * */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(MyJsonValidationException.class)
    public Object testMyValidationException(Exception exception, HttpServletResponse response){
        log.info("捕获到了异常,===>"+ exception);
        return ViewUtil.json(response,402,exception.getMessage());
    }@ExceptionHandler(TokenValidationException.class)
    public Object testTokenValidationException(Exception exception, HttpServletResponse response){
        log.info("捕获到了异常,===>"+ exception);
        return ViewUtil.json(response,401,exception.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Object testException(Exception exception, HttpServletResponse response){
        exception.printStackTrace();
        return ViewUtil.json(response,500,"程序异常");
    }
}
